/**

 @Name：layuiAdmin 用户管理 管理员管理 角色管理
 @Author：star1029
 @Site：http://www.layui.com/admin/
 @License：LPPL
    
 */


layui.define(['table', 'form', "tree", 'layer'], function(exports) {
	var $ = layui.$,
		setter = layui.setter,
		admin = layui.admin,
		view = layui.view,
		table = layui.table,
		tree = layui.tree,
		layer = layui.layer,
		form = layui.form;

	table.render({
		elem: '#LAY-evalutestudent-back-manage',
		method: "post",
		url: setter.api_source + 'evalute/studentList',
		parseData: function(res) { //res 即为原始返回的数据
			return {
				"code": res.code, //解析接口状态
				"msg": res.message, //解析提示文本
				"count": res.data.total, //解析数据长度
				"data": res.data.rows //解析数据列表
			};
		},
		cols: [
			[{
				field: 'courseName',
				title: '课程'
			}, {
				field: 'employeName',
				title: '教师'
			}, {
				field: 'clazzName',
				title: '班级',
				align: "center",
				templet: '<div><button class="layui-btn layui-btn-xs">{{d.clazzName}}</button></div>'
			}, {
				title: '操作',
				width: 250,
				align: 'center',
				fixed: 'right',
				toolbar: '#table-evalutestudent-admin'
			}]
		],
		text: '对不起，加载出现异常！',
		page: true,
		limit: 10,
		limits: [5, 10, 15],

	});

	// 初始化
	var addData = {};
	var questionNumber = 0;

	function initt() {
		/**
		 * 请求试题列表
		 * */
		questionNumber = 0;
		admin.req({
			type: "post",
			async:false,
			url: setter.api_source + 'evalute/studentgetItemListe',
			beforeSend: function(XMLHttpRequest) {

			},
			success: function(data, textStatus) {
				questionNumber = data.data.total;
				addData = data;
			},
			complete: function(XMLHttpRequest, textStatus) {
				//HideLoading();
			},
			error: function(data) {

			}
		});
	}


	//监听工具条
	table.on('tool(LAY-evalutestudent-back-manage)', function(obj) {

		if (obj.event === 'edit') {
			console.log("1");
			initt();
			admin.popup({
				title: '正在评教',
				area: ['1280px', '650px'],
				id: 'LAY-popup-evalutestudent-add',
				success: function(layero, index) {
					// 指定要渲染的模板html
					view(this.id).render('user/tes/evalute/student/evalutestudentquestion', addData).done(function() {
						// 指定渲染的fiter
						form.render(null, 'evalutestudent-edit-form');
						//监听提交
						form.on('submit(LAY-evalutestudent-back-submit)', function(data) {
							var field = data.field; //获取提交的字段
							console.log("2");
							//把每项问题的Id和其所选值存入map
							var map1 = new Map();
							var arr = new Array();
							var dataList = $('.checkList').find('input');
							for (var b = 0; b < dataList.length; b++) {
								if ($(dataList[b]).prop('checked')) {
									map1.set($(dataList[b])[0].name, $(dataList[b])[0].defaultValue);

									arr.push({
										"targetId": $(dataList[b])[0].name,
										"targetItemId": $(dataList[b])[0].defaultValue
									});
								}
							}
							if (questionNumber != map1.size) {
								layer.msg("请填写完所有选项");
							} else {
								admin.req({
									type: "post",
									contentType: 'application/json; charset=UTF-8',
									data: JSON.stringify({
										"empId": obj.data.employeId,
										"courseId": obj.data.courseId,
										"taskId": obj.data.taskId,
										"chooseVoList": arr,
									}),
									url: setter.api_source + 'evalute/submitanswer',
									beforeSend: function(XMLHttpRequest) {
										//ShowLoading();
									},
									success: function(data, textStatus) {
										layui.table.reload('LAY-evalutestudent-back-manage'); //重载表格
										layer.close(index); //执行关闭 
										layer.msg("评价成功");
									},
									complete: function(XMLHttpRequest, textStatus) {
										//HideLoading();
									},
									error: function() {
										//请求出错处理
									}
								});
							}
						});
					});
				}
			});
		} else if (obj.event === 'show') {
			console.log("列表");
			console.log(obj);
			admin.req({
				type: "post",
				data: {
					"empId": obj.data.employeId,
					"courseId": obj.data.courseId,
					"taskId": obj.data.taskId,
					"chooseVoList": [],
				},
				async: false,
				url: setter.api_source + 'evalute/studentgetItemListe',
				beforeSend: function(XMLHttpRequest) {
					//ShowLoading();
				},
				success: function(data, textStatus) {
					questionNumber = data.data.total;
					addData = data;
				},
				complete: function(XMLHttpRequest, textStatus) {
					//HideLoading();
				},
				error: function(data) {

				}
			});
			
			console.log("2");

			admin.popup({
				title: '评教结果',
				area: ['1280px', '650px'],
				id: 'LAY-popup-evalutestudent-add',
				success: function(layero, index) {
					// 指定要渲染的模板html
					view(this.id).render('user/tes/evalute/student/evalutestudentquestion', addData).done(function() {
						// 指定渲染的fiter
						form.render(null, 'evalutestudent-edit-form');
						//监听提交
						form.on('submit(LAY-evalutestudent-back-submit)', function(data) {
							layer.msg("已提交不可修改");
						});
					});
				}
			});
		}
	});

	exports('evalutestudent', {})
});
